package com.middlegroup.subjectpay.service.impl;

import com.middlegroup.subjectpay.config.ConstantInfo;
import com.middlegroup.subjectpay.entity.ThirdProtocolBindRecord;
import com.middlegroup.subjectpay.mapper.ThirdProtocolBindRecordMapper;
import com.middlegroup.subjectpay.service.ThirdProtocolBindRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chengweiwei123
 * @since 2019-01-24
 */
@Service
@Slf4j
public class ThirdProtocolBindRecordServiceImpl extends ServiceImpl<ThirdProtocolBindRecordMapper, ThirdProtocolBindRecord> implements ThirdProtocolBindRecordService {

    @Resource
    private ThirdProtocolBindRecordMapper thirdProtocolBindRecordMapper;

    @Autowired
    private ConstantInfo constantInfo;

    @Override
    public ThirdProtocolBindRecord getLastOne(ThirdProtocolBindRecord thirdProtocolBindRecord) {
        // 查询加密
        try {
            if (thirdProtocolBindRecord != null) {
                thirdProtocolBindRecord.SetEncryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        // 查询记录
        ThirdProtocolBindRecord record = thirdProtocolBindRecordMapper.getLastOne(thirdProtocolBindRecord);
        try {
            if (record != null) {
                record.GetDecryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素解密异常：{}", e);
        }
        return record;
    }

    @Override
    public boolean insert(ThirdProtocolBindRecord entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.insert(entity);
    }

    @Override
    public boolean updateById(ThirdProtocolBindRecord entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.updateById(entity);
    }

}
